<?php

namespace plugin\kb_blog\app\admin\controller;

use support\Request;
use support\Response;
use plugin\kb_blog\app\model\Option;

/**
 * 博客资料 class
 */
class InfoController
{

    /**
     * 资料设置页
     *
     * @return Response
     */
    public function index(): Response
    {
        return view('info/index');
    }

    /**
     * 获取设置
     *
     * @return Response
     */
    public function get(): Response
    {
        $setting = Option::all();
        $data = [];
        foreach ($setting as $k => $v) {
            $data[$v["name"]] = $v["value"];
        }
        $data = $data ? $data : [
            'title' => '快乐两小时',
            'keyword' => '',
            'description' => '🌿 放 弃 总 是 很 容 易 但 坚 持 是 痛 苦 的',
            'qrcode' => '/app/kb_blog/blog/images/qr_code.jpg',
            'api' => '',
            'icp' => '粤ICP备XXXXXXXXXX号',
            'min_title' => 'KingBes',
            'github' => '',
            'gitee' => '',
            'qq' => '1234567890',
            'wechat' => 'wechat',
        ];
        return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

    /**
     * 更改设置
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $param = $request->all();
        foreach ($param as $k => $v) {
            // 获取数据
            $find = Option::where("name", $k)->first();
            if ($find) {
                // 存在则更新
                $find->update(["value" => $v]);
            } else {
                // 不存在则新增
                Option::create(["name" => $k, "value" => $v]);
            }
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }
}
